package fr.utbm.gl52.smartgrid.view.listview;

import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.xml.transform.Source;

import fr.utbm.gl52.smartgrid.model.device.IDevice;
import fr.utbm.gl52.smartgrid.model.load.Load;
import fr.utbm.gl52.smartgrid.model.storage.AbstractStorage;
import fr.utbm.gl52.smartgrid.util.BiControllable;
import fr.utbm.gl52.smartgrid.util.Constants;

public class ListViewPanel extends JPanel implements BiControllable {
	/**
	 * 
	 */

	private static final long serialVersionUID = 2793916044993185503L;
	SubListPanel sources;
	SubListPanel loads;
	SubListPanel storages;

	public ListViewPanel() {
		this.sources = new SubListPanel(
				Messages.getString("ListViewPanel.0"), Constants.TYPE_SOURCE); //$NON-NLS-1$
		this.loads = new SubListPanel(
				Messages.getString("ListViewPanel.1"), Constants.TYPE_LOAD); //$NON-NLS-1$
		this.storages = new SubListPanel(
				Messages.getString("ListViewPanel.2"), Constants.TYPE_STORAGE); //$NON-NLS-1$

		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.add(this.sources);
		this.add(this.loads);
		this.add(this.storages);
	}

	public void addDevice(IDevice device) {
		if (device instanceof Load)
			this.loads.addDevice(device);
		else if (device instanceof Source)
			this.sources.addDevice(device);
		else if (device instanceof AbstractStorage)
			this.storages.addDevice(device);
	}

	public void update() {
		this.sources.update();
		this.loads.update();
		this.storages.update();
	}

	public void addController(ActionListener controller) {
		this.sources.addController(controller);
		this.loads.addController(controller);
		this.storages.addController(controller);
	}

	public void addControllers(ActionListener internalController,
			ActionListener externalController) {
		this.sources.addControllers(internalController, externalController);
		this.loads.addControllers(internalController, externalController);
		this.storages.addControllers(internalController, externalController);
	}

	public void update(Collection<IDevice> deviceList) {
		this.sources.reset();
		this.loads.reset();
		this.storages.reset();
		for (IDevice device : deviceList) {
			if (device != null) {
				Class<?> className = null;
				try {
					className = Class.forName(this.sources.getClassType());
				} catch (ClassNotFoundException e) {
					if (Constants.isDebug)
						System.out
								.println("Cannot find class from Sources List" //$NON-NLS-1$
										+ this.sources.getClassType());
					e.printStackTrace();
				}
				if (className != null
						&& className.isAssignableFrom(device.getClass())) {
					this.sources.addDevice(device);
				}

				try {
					className = Class.forName(this.loads.getClassType());
				} catch (ClassNotFoundException e) {
					if (Constants.isDebug)
						System.out.println("Cannot find class from Load List" //$NON-NLS-1$
								+ this.loads.getClassType());
					e.printStackTrace();
				}
				if (className != null
						&& className.isAssignableFrom(device.getClass())) {
					this.loads.addDevice(device);
				}

				try {
					className = Class.forName(this.storages.getClassType());
				} catch (ClassNotFoundException e) {
					if (Constants.isDebug)
						System.out
								.println("Cannot find class from Storage List" //$NON-NLS-1$
										+ this.storages.getClassType());
					e.printStackTrace();
				}
				if (className != null
						&& className.isAssignableFrom(device.getClass())) {
					this.storages.addDevice(device);
				}
			}
		}
		this.sources.update();
		this.loads.update();
		this.storages.update();
	}

}
